package cn.stylefeng.guns.topspeedCarRace;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Maps;
import lombok.Data;
import org.apache.commons.compress.utils.Lists;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Ao82 {
    private final static String url = "https://api.api68.com/klsf/getHistoryLotteryInfo.do?date={}&lotCode=10011";

    public static void main(String[] args) {
//        JSONObject jsonObject = readRace();
//        jisuan(jsonObject);
        getCodes("2022-11-10", 1);
    }

    public static void getCodes(String beginDate, int num) {
//        String beginDate = "2022-10-01";
        DateTime date = DateUtil.parse(beginDate);
        String format = "yyyy-MM-dd";
        Map<String, Map<String, List<String>>> maps = Maps.newLinkedHashMap();
        List<String> dates = Lists.newArrayList();
        HashBasedTable<String, String, StringBuffer> table = HashBasedTable.create();
        for (int i = 0; i < num; i++) {
            String dateStr = DateUtil.format(DateUtil.offsetDay(date, i), format);
            dates.add(dateStr);
            System.out.println(dateStr);
            JSONObject jsonObject = readRace(dateStr);
            jisuan(jsonObject, table);
        }
        print(table);
    }

    public static void print(HashBasedTable<String, String, StringBuffer> table) {
        for (int i = 0; i < 8; i++) {
            System.out.println("==============");
            Map<String, StringBuffer> row = table.row(String.valueOf(i));
            Iterator<String> iterator = row.keySet().iterator();
            while (iterator.hasNext()) {
                System.out.println(row.get(iterator.next()));
            }
        }
        System.out.println("==============");
//        System.out.println(row);
    }

    public static JSONObject readRace(String date) {

        String data = HttpUtil.createGet(StrUtil.format(url, date))
                .execute().body();
        JSONObject jsonObject = JSONObject.parseObject(data);
        return jsonObject;
    }

    public static void jisuan(JSONObject jsonObject, HashBasedTable<String, String, StringBuffer> table) {
        JSONArray datas = jsonObject.getJSONObject("result").getJSONArray("data");
        int ii = 1;
        for (int i = datas.size() - 1; i >= 0; i--) {
            JSONObject data = datas.getJSONObject(i);
            String code = data.getString("preDrawCode");
            String[] codes = code.split(",");
            System.out.println(CollUtil.join(Arrays.asList(codes), "\t"));

//            ii++;
//            for (int j = 0; j < codes.length; j++) {
//                StringBuffer sb = table.get(String.valueOf(j), String.valueOf(ii));
//                if (sb == null) {
//                    sb = new StringBuffer();
//                }
////                sb.append(codes[j]).append("\t").append("\t");
//                sb.append(codes[j]).append("\t");
//                table.put(String.valueOf(j), String.valueOf(ii), sb);
//            }
        }
    }

    public static void add(Map<Integer, Integer> map, Integer code) {
        for (int i = 1; i <= 10; i++) {
            if (i == code) {
                map.put(i, 0);
            } else {
                Integer num = map.get(i);
                map.put(i, ++num);
            }
        }
    }

    @Data
    public static class CarRace {
        private String issue;
        private Integer code1;
        private Integer code2;
        private Integer code3;
        private Integer code4;
        private Integer code5;
        private Integer code6;
        private Integer code7;
        private Integer code8;
        private String[] codes;

        public CarRace(String issue, Integer code1, Integer code2, Integer code3,
                       Integer code4, Integer code5, Integer code6, Integer code7,
                       Integer code8) {
            this.issue = issue;
            this.code1 = code1;
            this.code2 = code2;
            this.code3 = code3;
            this.code4 = code4;
            this.code5 = code5;
            this.code6 = code6;
            this.code7 = code7;
            this.code8 = code8;
        }

        public CarRace(String issue, Integer code1, Integer code2, Integer code3, Integer code4,
                       Integer code5, Integer code6, Integer code7, Integer code8, String[] codes) {
            this.issue = issue;
            this.code1 = code1;
            this.code2 = code2;
            this.code3 = code3;
            this.code4 = code4;
            this.code5 = code5;
            this.code6 = code6;
            this.code7 = code7;
            this.code8 = code8;
            this.codes = codes;
        }

        public Integer subIssue() {
            return Integer.valueOf(this.issue.substring(9));
        }
    }

    public static class LianXuCode {

    }
}
